package xyc.szk.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JdbcUtils3 {
    private static DataSource ds = null;

    static {
        //将jdbc.properties这个配置文件中key-value读到一个properties对象中
        Properties p = new Properties();
        //用类加载器来读取配置文件jdbc.properties
        ClassLoader cl = JdbcUtils3.class.getClassLoader();
        InputStream in = null;
        try {

            in = cl.getResourceAsStream("jdbc.properties");
            p.load(in);
            //创建一个阿里的数据源 druid
            DruidDataSource bds = new DruidDataSource();
            //DruidDataSourceFactory.createDataSource(p);
            bds.setDriverClassName(p.getProperty("jdbc.driver"));
            bds.setUrl(p.getProperty("jdbc.url"));
            bds.setUsername(p.getProperty("jdbc.username"));
            bds.setPassword(p.getProperty("jdbc.password"));
            ds = bds;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
    public  static DataSource getDataSource(){
        return ds;
    }
}
